package com.spring;

import java.nio.charset.Charset;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;
import com.spring.springCloud.TraceIdFeignHttpRequrestInterceptor;
import com.spring.springCloud.TraceIdHttpRequestInterceptor;

import feign.Request;
import feign.RequestInterceptor;

/**
 * Created by Harry on 2017/6/27.
 */
@Configuration
public class GlobalBean {

    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter() {
        StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return stringConverter;
    }

    @Bean
    public FastJsonHttpMessageConverter4 fastConverter() {
        FastJsonHttpMessageConverter4 fastConverter = new FastJsonHttpMessageConverter4();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //SerializerFeature.PrettyFormat,SerializerFeature.DisableCircularReferenceDetect
//        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue, 
//        		SerializerFeature.WriteNullBooleanAsFalse, 
//        		SerializerFeature.WriteNullListAsEmpty,
//        		SerializerFeature.WriteNullNumberAsZero,
//        		SerializerFeature.WriteNullStringAsEmpty);
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,
        		SerializerFeature.DisableCircularReferenceDetect);
        
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        fastConverter.setFastJsonConfig(fastJsonConfig);
        //UTF-8,by default
        fastConverter.setDefaultCharset(Charset.forName("UTF-8"));

        return fastConverter;
    }

    //For feignClient
    @Bean
    public RequestInterceptor requestInterceptor() {
        return new TraceIdFeignHttpRequrestInterceptor();
    }

    @Bean
    public TraceIdHttpRequestInterceptor traceIdHttpRequestInterceptor() {
        return new TraceIdHttpRequestInterceptor();
    }
    
    @Bean
    @ConditionalOnProperty(value={"spring.fegin.request.connectTimeoutMillis","spring.fegin.request.readTimeoutMillis"})
    @ConfigurationProperties(prefix = "spring.fegin.request")
    public Request.Options feignOptions(){
        return new Request.Options();
    }
    
    @Bean
    @ConditionalOnProperty(
    		value={"spring.rest.connection.connection-request-timeout",
    				"spring.rest.connection.connect-timeout",
    			"spring.rest.connection.read-timeout"}
    		)
    @ConfigurationProperties(prefix = "spring.rest.connection")
    public HttpComponentsClientHttpRequestFactory customHttpRequestFactory() {
        return new HttpComponentsClientHttpRequestFactory();
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(@Autowired HttpComponentsClientHttpRequestFactory customHttpRequestFactory,
                                     @Autowired FastJsonHttpMessageConverter4 fastConverter,
                                     @Autowired TraceIdHttpRequestInterceptor traceIdHttpRequestInterceptor ){

        RestTemplate restTemplate = new RestTemplate(customHttpRequestFactory);
        restTemplate.getMessageConverters().add(fastConverter);
        restTemplate.getInterceptors().add(traceIdHttpRequestInterceptor);

        return restTemplate;
    }

}